<?php

/**
 * @copyright   FOSP
 * @version     20120411
 * @todo        Mysql Access to table rating_player_statistic
 * @name        Fpt_Business_RatingPlayerStatistic_Adapter_Mysql
 * @author      QuangTM 
 */
class Fpt_Business_RatingPlayerStatistic_Adapter_Mysql extends Fpt_Business_RatingPlayerStatistic_Adapter_Abstract
{

    /**
     * Get Fpt_Business_RatingPlayerStatistic_Adapter_Mysql's instance
     * @return Fpt_Business_RatingPlayerStatistic_Adapter_Mysql
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get statistic rating player
     * @param int $matchID
     * @return array|boolean 
     * @author QuangTM
     */
    public function getRatingPlayerStatistic($matchID)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_sport_getRatingPlayerStatistic(:p_matchid);');

            // Bind param
            $stmt->bindParam('p_matchid', $matchID, PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Get result
            $result = array();

            // Get format data
            $formatInstance = new Fpt_Business_RatingPlayerStatistic_Metadata_RatingPlayerStatistic();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $result[$row['player_id']] = $formatInstance->init($row)->getFormatedData();
            }

            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt, $formatInstance);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Insert/Update rating player statistic
     * @param array $params
     * @return int|boolean 
     * @author QuangTM
     */
    public function insertRatingPlayerStatistic($params)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_sport_insertRatingPlayerStatistic(:p_matchid, :p_teamid, :p_playerid, :p_ratinguser, :p_ratingvne);');

            $null = NULL;

            // Bind param
            $stmt->bindParam('p_matchid', $params['MatchID'], PDO::PARAM_INT);
            $stmt->bindParam('p_teamid', $params['TeamID'], PDO::PARAM_INT);
            $stmt->bindParam('p_playerid', $params['PlayerID'], PDO::PARAM_INT);
            $stmt->bindParam('p_ratinguser', $params['RatingUser'], PDO::PARAM_INT);
            if (isset($params['RatingVne']))
                $stmt->bindParam('p_ratingvne', $params['RatingVne'], PDO::PARAM_INT);
            else
                $stmt->bindParam('p_ratingvne', $null, PDO::PARAM_NULL);

            // Execute
            $stmt->execute();

            // Get result
            $result = $stmt->fetchColumn();

            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}